package view.frontend.order;

import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import view.ViewSingleton;
import view.generaldesign.DesignLayout;

/*
 @author Mathias and Sonny Sandberg
 */ 
public class OrderDate {

    private JPanel datePanel;
    private JTextField tFieldPickUp,
            tFieldDelivery;
    private Order order;

    public OrderDate(Order view) {
        order = view;

        datePanel = new JPanel(null);
        datePanel.setBounds(0, 0, 300, 135);
        datePanel.setOpaque(false);
        datePanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Dato", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));

        JLabel lblPickUp = new JLabel("Afhentning");
        lblPickUp.setBounds(10, 25, 280, 18);
        lblPickUp.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));

        JLabel lblDelivery = new JLabel("Levering");
        lblDelivery.setBounds(10, 75, 280, 18);
        lblDelivery.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));

        tFieldPickUp = new JTextField();
        tFieldPickUp.setBounds(10, 45, 280, 25);
        tFieldPickUp.setBorder(DesignLayout.T_FIELD_RED);
        tFieldPickUp.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldPickUp.setFont(DesignLayout.STANDARD);
        tFieldPickUp.setEditable(false);
        tFieldPickUp.addMouseListener( new MouseAdapter()
        {
            @Override
            public void mouseReleased(MouseEvent evt)
            {
                tFieldPickUp.setText(new utility.DateChooser(ViewSingleton.getSingletonObject().getMainFrame()).setPickedDate());
                order.getOrderObject().setPickup_date(tFieldPickUp.getText());
            }
        });

        tFieldDelivery = new JTextField();
        tFieldDelivery.setBounds(10, 95, 280, 25);
        tFieldDelivery.setBorder(DesignLayout.T_FIELD_RED);
        tFieldDelivery.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldDelivery.setFont(DesignLayout.STANDARD);
        tFieldDelivery.setEditable(false);
        tFieldDelivery.addMouseListener( new MouseAdapter()
        {
            @Override
            public void mouseReleased(MouseEvent evt)
            {
                tFieldDelivery.setText(new utility.DateChooser(ViewSingleton.getSingletonObject().getMainFrame()).setPickedDate());
                order.getOrderObject().setDelivery_date(tFieldDelivery.getText());
            }
        });

        datePanel.add(lblPickUp);
        datePanel.add(lblDelivery);
        datePanel.add(tFieldPickUp);
        datePanel.add(tFieldDelivery);

        datePanel.setVisible(false);
    }
    
    public void setFieldsFromModel()
    {
        model.Order orderObject = order.getOrderObject();
        tFieldPickUp.setText(orderObject.getPickup_date());
        tFieldDelivery.setText(orderObject.getDelivery_date());
    }

    public JPanel getPanel() {
        return datePanel;
    }
}